package com.ruoyi.wb.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbElecSafety;
import com.ruoyi.wb.domain.WbEntAuth;
import com.ruoyi.wb.domain.WbMaintainCompany;
import com.ruoyi.wb.enmus.RoleNameEnums;
import com.ruoyi.wb.mapper.WbElecSafetyMapper;
import com.ruoyi.wb.service.IWbElecSafetyService;
import com.ruoyi.wb.service.IWbEntAuthService;
import com.ruoyi.wb.service.IWbMaintainCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 用电安全Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-14
 */
@Service
public class WbElecSafetyServiceImpl implements IWbElecSafetyService {
    @Autowired
    private WbElecSafetyMapper wbElecSafetyMapper;

    @Autowired
    private IWbEntAuthService iWbEntAuthService;

    @Autowired
    private IWbMaintainCompanyService iWbMaintainCompanyService;

    /**
     * 查询用电安全
     *
     * @param id 用电安全主键
     * @return 用电安全
     */
    @Override
    public WbElecSafety selectWbElecSafetyById(Long id) {
        return wbElecSafetyMapper.selectWbElecSafetyById(id);
    }

    /**
     * 查询用电安全列表
     *
     * @param wbElecSafety 用电安全
     * @return 用电安全
     */
    @Override
    public List<WbElecSafety> selectWbElecSafetyList(WbElecSafety wbElecSafety) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<WbElecSafety> wbElecSafeties = new ArrayList<>();

        List<SysRole> roles = sysUser.getRoles();
        for (SysRole role : roles) {
            if (role.getRoleName().equals(RoleNameEnums.ADMIN.getDescribe())||
                role.getRoleName().equals(RoleNameEnums.FIRE_UNIT_MANAGER.getDescribe())||
                role.getRoleName().equals(RoleNameEnums.FIREFIGHTER.getDescribe())
            ) { //超级管理员 消防单位
                startPage();
                wbElecSafeties = wbElecSafetyMapper.selectWbElecSafetyList(wbElecSafety);
            } else if (role.getRoleName().equals(RoleNameEnums.MAINTAIN_ADMIN.getDescribe())) {//维保公司管理员
                List<WbMaintainCompany> wbMaintainCompanies = iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(sysUser.getUserId());
                if (!CollectionUtils.isEmpty(wbMaintainCompanies)) {
                    wbElecSafety.setMaintainCompanyId(wbMaintainCompanies.get(0).getId());
                    startPage();
                    wbElecSafeties = wbElecSafetyMapper.selectWbElecSafetyList(wbElecSafety);
                }
            } else { //普通用户
                //查询自己企业id
                List<WbEntAuth> wbEntAuthList = iWbEntAuthService.selectWbEntAuthListByUserId(sysUser.getUserId());
                if (!CollectionUtils.isEmpty(wbEntAuthList)) {
                    wbElecSafety.setEnterpriseId(wbEntAuthList.get(0).getId());
                    startPage();
                    wbElecSafeties = wbElecSafetyMapper.selectWbElecSafetyList(wbElecSafety);
                }
            }
        }


        return wbElecSafeties;
    }

    /**
     * 新增用电安全
     *
     * @param wbElecSafety 用电安全
     * @return 结果
     */
    @Override
    public int insertWbElecSafety(WbElecSafety wbElecSafety) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<WbMaintainCompany> wbMaintainCompanies = iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(sysUser.getUserId());
        if (!CollectionUtils.isEmpty(wbMaintainCompanies)) {
            wbElecSafety.setMaintainCompanyId(wbMaintainCompanies.get(0).getId());
        }
        wbElecSafety.setCreateTime(DateUtils.getNowDate());
        wbElecSafety.setCreateBy(sysUser.getNickName());
        wbElecSafety.setUpdateBy(sysUser.getNickName());
        wbElecSafety.setUpdateTime(DateUtils.getNowDate());
        return wbElecSafetyMapper.insertWbElecSafety(wbElecSafety);
    }

    /**
     * 修改用电安全
     *
     * @param wbElecSafety 用电安全
     * @return 结果
     */
    @Override
    public int updateWbElecSafety(WbElecSafety wbElecSafety) {
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        wbElecSafety.setUpdateTime(DateUtils.getNowDate());
        wbElecSafety.setUpdateBy(loginUser.getNickName());
        return wbElecSafetyMapper.updateWbElecSafety(wbElecSafety);
    }

    /**
     * 批量删除用电安全
     *
     * @param ids 需要删除的用电安全主键
     * @return 结果
     */
    @Override
    public int deleteWbElecSafetyByIds(Long[] ids) {
        return wbElecSafetyMapper.deleteWbElecSafetyByIds(ids);
    }

    /**
     * 删除用电安全信息
     *
     * @param id 用电安全主键
     * @return 结果
     */
    @Override
    public int deleteWbElecSafetyById(Long id) {
        return wbElecSafetyMapper.deleteWbElecSafetyById(id);
    }
}
